package cvsexercise9;

public class BasicPolymorphism {

	/* Constant for the total size of the array */
	public final static int TOTAL_SHAPES = 5;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/* Write out a start message to the console */
		System.out.println("Exercise 9 - Polymorphism started");
		
		/* Create an array of 5 Shape objects */
		 Shape[] myShapeArray = new Shape[TOTAL_SHAPES];
		 
		 /* Add a mixture of Triangle objects and Rectangle objects
		  * NOTE: use a range of colours and sizes when instantiating the objects
		  */
		 myShapeArray[0] = new Rectangle("red", 2.0, 0.5);
		 myShapeArray[1] = new Rectangle("green", 1.5, 1.0);
		 myShapeArray[2] = new Triangle("blue", 0.5, 2.5);
		 myShapeArray[3] = new Rectangle("white", 1.5, 2.0);
		 myShapeArray[4] = new Triangle("black", 1.5, 3.0);

		 /* For each object in the Shape array, determine its type (Rectangle or Triangle), its
		  * length and width (if Rectangle), or base and height (if Triangle), and its area, then
		  * write out the type, dimensions and area to the console output
		  * NOTE: the "instanceof" operator should be used to test for the type of the Shape object
		  * NOTE: a downcast is required over the array instance to use the class specific methods
		  */
		 for (int idx = 0; idx < myShapeArray.length; idx++) {
			 
			 if (myShapeArray[idx] instanceof Rectangle) {
				 double dLength = ((Rectangle) myShapeArray[idx]).getLength();
				 double dWidth = ((Rectangle) myShapeArray[idx]).getWidth();
				 System.out.print("myShapeArray[" + idx + "] is a Rectangle: ");
				 System.out.print("Length = " + dLength + ", Width = " + dWidth);
			 } else {
				 double dBase = ((Triangle) myShapeArray[idx]).getBase();
				 double dHeight = ((Triangle) myShapeArray[idx]).getHeight();
				 System.out.print("myShapeArray[" + idx + "] is a Triangle: ");
				 System.out.print("Base = " + dBase + ", Height = " + dHeight);
			 }
			 
			 double dArea = myShapeArray[idx].getArea();
			 System.out.println(", Area = " + dArea);
		 }
		
		/* Write out a completed message to the console */
		System.out.println("Exercise 9 - Polymorphism completed");
	}
}
